package com.sisyphus.spinach.network.utils;

import android.os.Looper;
import android.util.Log;

import com.sisyphus.spinach.core.INetTask;

import androidx.annotation.NonNull;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.ObservableTransformer;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class RxUtil {

    private static final String TAG = "SchedulersTransformer";


    /**
     * 线程调度
     */
    public static <T> ObservableTransformer<T, T> transformer() {

        return upstream -> upstream.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }


    /**
     * 带状态的线程调度
     */
    public static <T> ObservableTransformer<T, T> transformer(@NonNull INetTask.View<T> view) {
        // 防止不在主线，调用闪退
        if (Looper.getMainLooper() == Looper.myLooper()) {
            view.start();

        } else {
            Log.e(TAG, "transformer(IStatusView view) \t" + Thread.currentThread().getName());
        }

        return transformer();
    }

}